package com.gin.kotlin.no_gay_alliance.app

import android.app.Application
import android.app.Notification
import android.os.Handler
import android.os.Message
import android.util.Log
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.HandlerHolder
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.NotificationUtils
import com.gin.kotlin.no_gay_alliance.database.NgaDatabase
import com.gin.kotlin.no_gay_alliance.database.entity.NgaAccount
import com.gin.kotlin.no_gay_alliance.enums.NotificationType
import com.gin.kotlin.no_gay_alliance.model.AvatarBuffModel
import com.gin.kotlin.no_gay_alliance.model.emoji.EmojiPackageModel
import com.gin.kotlin.no_gay_alliance.model.emoji.GenericEmojiGroup
import com.gin.kotlin.no_gay_alliance.model.emoji.GenericEmojiPackage
import com.gin.kotlin.no_gay_alliance.runner.AccountManagerRunner
import com.gin.kotlin.no_gay_alliance.runner.ClearCacheRunner
import com.gin.kotlin.no_gay_alliance.runner.DatabaseRunner
import com.gin.kotlin.no_gay_alliance.runner.PhotoPreviewRunner
import com.gin.kotlin.no_gay_alliance.runner.ResourceRunner
import com.gin.kotlin.no_gay_alliance.runner.WorkerRunner
import com.gin.nga.api.ResourceApi
import com.gin.nga.main.NgaApi
import com.gin.nga.response.resource.bbscode.EmoteGroup
import com.gin.nga.response.user.BlockConfig

/**
 * APP对象
 * @property TAG TAG
 * @property handler [MyHandler]
 * @property versionName APP当前版本
 * @property resourceApi 静态资源API
 * @property database 数据库连接
 * @property account 当前账号
 * @property ngaApi 当前账号的api
 * @property emoteGroup 官方表情包
 * @property avatarBuffs 头像buff
 * @property emojiPackages 第三方表情包
 * @property blockConfig 屏蔽数据
 */
class App : Application(), HandlerHolder, Handler.Callback {
    @Suppress("PrivatePropertyName")
    private val TAG = App::class.java.simpleName
    override var handler: MyHandler? = null
    lateinit var versionName: String
    lateinit var resourceApi: ResourceApi
    lateinit var database: NgaDatabase
    lateinit var account: NgaAccount
    lateinit var ngaApi: NgaApi
    var emoteGroup: List<EmoteGroup>? = null
    var avatarBuffs: List<AvatarBuffModel>? = null
    var emojiPackages: MutableList<EmojiPackageModel>? = null
    var blockConfig: BlockConfig? = null

    /**
     * 获取通用表情包
     * @return MutableList<GenericEmojiGroup>
     */
    fun obtainGenericEmojiPackages() = mutableListOf<GenericEmojiPackage>().also { list ->
        val packageName = "官方表情包"
        GenericEmojiPackage(packageName, emoteGroup?.map { GenericEmojiGroup(it, packageName = packageName) }).also { list.add(it) }
        emojiPackages?.map { GenericEmojiPackage(it) }?.also { list.addAll(it) }
    }

    /**
     * 发送通知推送
     * @param type 通知类型
     * @param message 消息内容
     * @param cls 点击消息后打开的活动类对象
     */
    fun notify(type: NotificationType, message: String, cls: Class<*>) {
        Log.i(TAG, "消息推送: $message")
        val channel = NotificationUtils.getChannel(this, type.channelId)
        val notification = Notification.Builder(this, channel.id)
            .setSmallIcon(R.drawable.ic_app_k)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(NotificationUtils.buildPendingIntent(this, cls))
            .build()
        // 发布推送
        NotificationUtils.notify(this, type.ordinal, notification)
    }



    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
        // 当前版本号
        this.versionName = packageManager.getPackageInfo(packageName, 0).versionName

        Log.d(TAG, "[APP] APP当前版本: " + this.versionName)
        this.handler = MyHandler(this, this)

        // 初始化任务
        listOf(
            // 数据库连接
            DatabaseRunner(),
            //账号管理器
            AccountManagerRunner(),
            // 静态资源
            ResourceRunner(),
            // 初始化 PhotoPreview
            PhotoPreviewRunner(),
            // 清理缓存
            ClearCacheRunner(),
            WorkerRunner(),
        ).forEach { it.run(this) }
    }



    companion object {
        /**
         * 下载目录
         */
        const val DOWNLOAD_DIR = "no_gay_kot/"
        const val KB = 1024L
        const val MB = KB * KB

        /**
         * 将APP实例保存到静态变量
         */
        lateinit var INSTANCE: App
    }

    /**
     * 处理消息
     * @param msg Message
     * @return Boolean
     */
    override fun handleMessage(msg: Message) = true
}